#!/usr/bin/python
# -*- coding: utf-8 -*-  
"""
@Project : hello 
@file : optimized_concat.py
@Author : sheen
@time : 2025/5/20 15:47
@func :
"""

import logging
import pandas as pd
from datetime import datetime
from typing import List, Dict
from com.cn.for_cdc.common.log_helper import configure_logging

configure_logging()

def process_data(
    pass_dicts: List[Dict[int, int]],
    fail_dicts: List[Dict[int, int]],
    missing_keys: List[int]
) -> pd.DataFrame:
    """处理原始数据生成DataFrame"""
    # 转换为查询字典
    pass_dict = {k: v for d in pass_dicts for k, v in d.items()}
    fail_dict = {k: v for d in fail_dicts for k, v in d.items()}
    missing_set = set(missing_keys) - pass_dict.keys() - fail_dict.keys()

    # 生成排序键列表
    all_keys = list(pass_dict.keys()) + list(fail_dict.keys()) + list(missing_set)
    sorted_keys = sorted(set(all_keys))

    # 向量化处理状态和值
    statuses, values = [], []
    for key in sorted_keys:
        if key in pass_dict:
            statuses.append("Pass")
            values.append(pass_dict[key])
        elif key in fail_dict:
            statuses.append("Fail")
            values.append(fail_dict[key])
        else:
            statuses.append("Missing")
            values.append(pd.NA)

    return pd.DataFrame({
        "Key": sorted_keys,
        "Status": pd.Categorical(statuses),
        "Value": pd.to_numeric(values, errors='coerce')
    })

def export_results(df: pd.DataFrame) -> None:
    """导出结果文件"""
    filename = f"{datetime.now().strftime('%Y%m%d%H%M%S')}_results.csv"
    df.to_csv(
        filename,
        index=False,
        encoding='utf-8-sig',
        float_format="%.0f"  # 整数格式输出
    )
    logging.info("文件已生成：%s，记录数：%d", filename, len(df))

if __name__ == "__main__":


    list_pass=[{18: 0}, {26: 0}, {31: 0}, {32: 0}, {35: 0}, {39: 0}]
    list_fail=[{3: 2138717}, {4: 6362001}, {5: 6361514}, {6: 4186534}, {7: 5301538}, {8: 5301026}, {9: 5300666}, {10: 5300141},
     {11: 5297207}, {12: 5293996}, {13: 5285817}, {14: 5257453}, {15: 5257394}, {16: 5257117}, {19: 328260},
     {20: 5403171}, {21: 5403169}, {22: 5403333}, {23: 5403169}, {24: 5403171}, {25: 5403199}, {27: 5403169},
     {28: 5403207}, {29: 5522235}, {30: 7185122}, {33: 5523143}, {34: 21556}, {36: 25943}, {37: 1230}, {40: 39315436},
     {41: 36826609}, {42: 36826605}, {43: 36826609}, {44: 36826609}, {45: 36826609}, {47: 153071}, {51: 14381495}]

    missing_keys=[17, 38, 46, 48, 49, 50]



    # 执行处理流程
    try:
        df = process_data(list_pass, list_fail, missing_keys)
        if not df.empty:
            # export_results(df)
            logging.info(df)
            logging.info("数据处理完成")
        else:
            logging.warning("生成空数据集")
    except Exception as e:
        logging.error("数据处理失败: %s", str(e), exc_info=True)
        raise